
#include	<cstring>
#include	<string>
#include	"IA.hh"

IA::IA(const char *serverIp_)
  :localIp(sf::IPAddress::GetLocalAddress()), serverIp(serverIp_)
{
}

bool		IA::connectServer(const char *ip, const char *port)
{
  if (me.Connect(atoi(port), ip) != sf::Socket::Done)
    {
      std::cerr << "Unabkle to connect to remote server exiting now" << std::cout;
      return false;
    }
  return true;
}

bool		IA::discoverServer(void)
{
  char		*buffer;
  size_t		x;
  // std::string	tmp, tmp2;

  buffer = (char *)malloc(1024);
  memset(buffer, 0, 1024);
  this->packet << "CONNECT CLIENT\n";
  if (this->me.Send(this->packet) != sf::Socket::Done)
    {
      std::cerr << "Unable to write on socket exiiting now" << std::endl;
      exit(-1);
    }
  this->packet.Clear();
  // do
  //   {
  //     this->me.Receive(this->packet);
  //     std::cout << this->packet.GetDataSize() << std::endl;
  //     this->packet >> tmp2;
  //     tmp.append(tmp2);
  //     std::cout << tmp << ":" << tmp2 << " " << tmp.size() << std::endl;
  //   } while (this->packet.GetDataSize() == 0);
  //while ((tmp.size() > 0 && tmp.at(tmp.size() - 1) != '\n') || tmp.size() <= 0);
  while (!(strlen(buffer)))
    this->me.Receive(buffer, 128, x);
  std::cout << buffer << std::endl;
  // std::cout << x << std::endl;
  // if (!strncmp("RULES", tmp.c_str(), 5))
  //   {
  //     std::cout << "+6 " << tmp.c_str() + 6 << std::endl;
  //     if (!(strncmp("0", tmp.c_str() + 6, 1)))
  // 	this->datas.setRule3(false);
  //     else
  // 	this->datas.setRule3(true);
  //     if (!(strncmp("0", tmp.c_str() + 8, 1)))
  // 	this->datas.setRule5break(false);
  //     else
  // 	this->datas.setRule5break(true);
  //     this->datas.setTime(atoi(tmp.c_str() + 10));
  //   }
  // std::cout << this->datas.getRule3() << " " <<  this->datas.getRule5break() << " " << this->datas.getTime() << std::endl;
  return true;
}

bool		IA::Data::getRule3(void) const
{
  return (this->rule3);
}

void		IA::Data::setRule3(bool v)
{
  this->rule3 = v;
}

bool		IA::Data::getRule5break(void) const
{
  return (this->rule5break);
}

void		IA::Data::setRule5break(bool v)
{
  this->rule5break = v;
}

size_t		IA::Data::getTime(void) const
{
  return (this->time);
}

void		IA::Data::setTime(size_t v)
{
  this->time = v;
}

